#define  _CRT_SECURE_NO_WARNINGS
// class Solution {
// public:
//     int longestSubsequence(vector<int>& arr, int difference) {
//         int n = arr.size(), ret = 1;
//         vector<int> dp(n, 1);
//         for(int i = 1; i < n; i++)
//             for(int j = i - 1; j >= 0; j--)
//                 if(arr[i] - arr[j] == difference){
//                     ret = max(ret, dp[i] = max(dp[i], 1 + dp[j]));
//                     break;
//                 }
//         return ret;
//     }
// };
class Solution {
public:
    int longestSubsequence(vector<int>& arr, int difference) {
        int n = arr.size(), ret = 1;
        unordered_map<int, int> hash;
        for (int i = 0; i < n; i++)
            ret = max(ret, hash[arr[i]] = 1 + hash[arr[i] - difference]);
        return ret;
    }
};